package core;

import java.util.logging.Level;
import java.util.logging.Logger;
import model.graphic.objects.GInputPort;
import view.GUI.graphic.GraphicView;
import controller.BidirectionalHashMap;
import controller.Constants;
import javax.swing.JTextArea;
import model.graphic.objects.GConnection;
import model.graphic.objects.GHandleConnection;
import model.graphic.objects.GTrigger;
import model.graphic.objects.GTriggerConnection;
import model.graphic.objects.Graphical;
import view.GUI.LabelReader;

/**
 * Singleton class for tracking messages between ports (call or get)
 * 
 * @author Wijnand
 */
public class HighLightTracker extends Tracker {

    private GraphicView gview;
    private BidirectionalHashMap sync;
    private JTextArea debug;

    
    public HighLightTracker(GraphicView gv, JTextArea debug) {
        this.gview = gv;
        this.debug = debug;
    }

    @Override
    public void valueRequested(ValuePort port) {
        sync = BidirectionalHashMap.getInstance();
        if (InPort.class.isAssignableFrom(port.getClass()) && sync.get(port) != null && GConnection.class.isAssignableFrom((sync.get(port)).getClass())) {
            GHandleConnection g = (GHandleConnection) sync.get(port);
            GInputPort gi = g.getInput();
            try {
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getPausetime());
            } catch (InterruptedException ex) {
                Logger.getLogger(HighLightTracker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        debug.append(LabelReader.getInstance().getString("Port") + "  " + port + ": " + LabelReader.getInstance().getString("requestedValue"));
    }

    @Override
    public void valueDispatched(OutPort source, Object value) {
        sync = BidirectionalHashMap.getInstance();
        if (InPort.class.isAssignableFrom(source.getRequestPort().getClass()) && sync.get(source.getRequestPort()) != null && GConnection.class.isAssignableFrom((sync.get(source.getRequestPort())).getClass())) {
            GHandleConnection g = (GHandleConnection) sync.get(source.getRequestPort());
            Graphical gi = g.getOutput();
            try {
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getPausetime());
            } catch (InterruptedException ex) {
                Logger.getLogger(HighLightTracker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        debug.append("OutPort " + source + ": " + LabelReader.getInstance().getString("dispatched") + " " + value+"\n");
    }

    @Override
    public void valueReceived(InPort target, Object value) {
        debug.append("InPort " + target + ": " + LabelReader.getInstance().getString("received")+ " " + value+"\n");
    }

    @Override
    public void startDispatch(DispatcherPort dispatcher, TriggerPort target) {
        sync = BidirectionalHashMap.getInstance();
        if (sync.get(target) != null && GConnection.class.isAssignableFrom((sync.get(target)).getClass())) {
            GTriggerConnection g = (GTriggerConnection) sync.get(target);
            GTrigger gi = g.getTrigger();
            try {
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(true);
                gi.setHighlighted(true);
                gview.giveAttention();
                Thread.sleep(Constants.getHighlighttime());
                g.setHighlighted(false);
                gi.setHighlighted(false);
                gview.giveAttention();
                Thread.sleep(Constants.getPausetime());
            } catch (InterruptedException ex) {
                Logger.getLogger(HighLightTracker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        debug.append("DispatcherPort " + dispatcher + ": " + LabelReader.getInstance().getString("dispatchingStarted"));
    }

    @Override
    public void stopDispatch(DispatcherPort dispatcher) {
        debug.append("DispatcherPort " + dispatcher + ": " + LabelReader.getInstance().getString("dispatchingStopped"));
    }

    @Override
    public void startCall(TriggerPort triggerPort) {
        debug.append("TriggerPort " + triggerPort + ": " + LabelReader.getInstance().getString("callStarted"));
    }

    @Override
    public void stopCall(TriggerPort triggerPort) {
        debug.append("TriggerPort " + triggerPort + ": " + LabelReader.getInstance().getString("callStopped"));
    }
}
